package com.googlecode.continuity.plugin.svnbrowser.client.wrapper;

import java.util.Date;

import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * This is a copy of the class org.tmatesoft.svn.core.SVNDirEntry, with the exception of comparable-ness
 * and the SVNURL property. Some fields may themselves be wrappers. Used to represent SVNDirEntries in GWT code.
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class SVNDirEntryWrapper implements IsSerializable {
  private String myName;
  private int myKind;
  private long mySize;
  private boolean myHasProperties;
  private long myRevision;
  private Date myCreatedDate;
  private String myLastAuthor;
  private String myPath;
  private String myCommitMessage;
  private SVNLockWrapper myLock;
  private String myURL;

  /**
   * Constructs an instance of <b>SVNDirEntryWrapper</b>.
   *
   * @param name          an entry name
   * @param kind          the node kind for the entry
   * @param size          the entry size in bytes
   * @param hasProperties <span class="javakeyword">true</span> if the
   *                      entry has properties, otherwise <span class="javakeyword">false</span>
   * @param revision      the last changed revision of the entry
   * @param createdDate   the date the entry was last changed
   * @param lastAuthor    the person who last changed the entry
   * @param URL           complete URL to the entry
   */
  public SVNDirEntryWrapper(String name, int kind, long size,
                            boolean hasProperties, long revision, Date createdDate,
                            String lastAuthor, String URL) {
    myName = name;
    myKind = kind;
    mySize = size;
    myHasProperties = hasProperties;
    myRevision = revision;
    myCreatedDate = createdDate;
    myLastAuthor = lastAuthor;
    myURL = URL;
  }

  public SVNDirEntryWrapper() {

  }

  /**
   * Gets the the directory entry name
   *
   * @return the name of this entry
   */
  public String getName() {
    return myName;
  }

  /**
   * Returns the file size in bytes (if this entry is a file).
   *
   * @return the size of this entry in bytes
   */
  public long getSize() {
    return mySize;
  }

  /**
   * Returns the file size in bytes (if this entry is a file).
   *
   * @return the size of this entry in bytes
   * @deprecated use {@link #getSize()} instead
   */
  public long size() {
    return getSize();
  }

  /**
   * Tells if the entry has any properties.
   *
   * @return <span class="javakeyword">true</span> if has,
   *         <span class="javakeyword">false</span> otherwise
   */
  public boolean hasProperties() {
    return myHasProperties;
  }

  /**
   * Returns the entry node kind.
   *
   * @return the node kind of this entry
   */
  public int getKind() {
    return myKind;
  }

  /**
   * Returns the date the entry was last changed.
   *
   * @return the datestamp when the entry was last changed
   */
  public Date getDate() {
    return myCreatedDate;
  }

  /**
   * Gets the last changed revision of this entry.
   *
   * @return the revision of this entry when it was last changed
   */
  public long getRevision() {
    return myRevision;
  }

  /**
   * Retrieves the name of the author who last changed this entry.
   *
   * @return the last author's name.
   */
  public String getAuthor() {
    return myLastAuthor;
  }

  /**
   * Returns the entry's path.
   * <p/>
   * <p/>
   * This method always returns the name of an entry (i.e.
   * a path relative to the parent folder) when an <b>SVNDirEntry</b>
   * object is provided by an {@link org.tmatesoft.svn.core.io.SVNRepository}
   * driver.
   * <p/>
   * <p/>
   * This property (relative path) is longer than just an entry name only when
   * an <b>SVNDirEntry</b> object is obtained via a recursive call to
   * a <code>doList()</code> method of the {@link org.tmatesoft.svn.core.wc.SVNLogClient} class.
   * In that case an <b>SVNDirEntry</b> object located deep in the hierarchy
   * will return a path relative to the URL <code>doList()</code> was called for.
   *
   * @return a path relative to a repository location or
   *         <span class="javakeyword">null</span> if no path is
   *         specified
   */
  public String getRelativePath() {
    return myPath == null ? getName() : myPath;
  }

  /**
   * Returns the commit log message for the revision of this entry.
   *
   * @return a commit log message
   */
  public String getCommitMessage() {
    return myCommitMessage;
  }

  /**
   * Gets the lock object for this entry (if it's locked).
   *
   * @return a lock object or <span class="javakeyword">null</span>
   */
  public SVNLockWrapper getLock() {
    return myLock;
  }

  /**
   * This method is used by SVNKit internals and not intended for users (from an API point of view).
   *
   * @param path this entry's path
   */
  public void setRelativePath(String path) {
    myPath = path;
  }

  /**
   * This method is used by SVNKit internals and not intended for users (from an API point of view).
   *
   * @param message a commit message
   */
  public void setCommitMessage(String message) {
    myCommitMessage = message;
  }

  /**
   * Sets the lock object for this entry (if it's locked).
   *
   * @param lock a lock object
   */
  public void setLock(SVNLockWrapper lock) {
    myLock = lock;
  }

  public String getURL() {
    return myURL;
  }

  /**
   * Returns a string representation of this object.
   *
   * @return a string representation of this directory entry
   */
  public String toString() {
    StringBuffer result = new StringBuffer();
    result.append("name=");
    result.append(myName);
    result.append(", kind=");
    result.append(myKind);
    result.append(", size=");
    result.append(mySize);
    result.append(", hasProps=");
    result.append(myHasProperties);
    result.append(", lastchangedrev=");
    result.append(myRevision);
    if (myLastAuthor != null) {
      result.append(", lastauthor=");
      result.append(myLastAuthor);
    }
    if (myCreatedDate != null) {
      result.append(", lastchangeddate=");
      result.append(myCreatedDate);
    }
    return result.toString();
  }
}
